<?php
class db extends PDO{

    public function __construct()
    {
        $dbname = "D_PLUG";
        $user = "d_plug";
        $password = "hi-gog";
        parent::__construct("mysql:dbname={$dbname};hostname=localhost;charset=utf8", $user, $password);
        $this->query("SET NAMES UTF8");
        
        
    }

    public function objectsQuery($query,params $params=null){
                       
        $statement=$this->prepare($query);
        if ($params != null)
        {
            foreach ($params->list as $param)
            {
                $statement->bindParam($param["k"], $param["v"], empty($param["t"]) ? PDO::PARAM_STR : $param["t"]);
            }
        }
        $statement->execute();
        
        return $statement->fetchObject();
        
    }
    
    public function singleQuery($query, params $params=null)
    {
        $statement = $this->prepare($query);

        if ($params != null)
        {
            foreach ($params->list as $param)
            {
                $statement->bindParam($param["k"], $param["v"], empty($param["t"]) ? PDO::PARAM_STR : $param["t"]);
            }
        }
        $statement->execute();

        return $statement->fetchColumn(0);
    }
}

class params{
    
    public function add($key,$value,$type=PDO::PARAM_STR){
        $this->list[]=array(
            "k"=>$key,
            "v"=>$value,
            "t"=>$type
            );
    }
    public $list=array();    
}
